<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="职级名称" prop="rankName">
        <el-input
          v-model="queryParams.rankName"
          placeholder="请输入职级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pay:rank:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pay:rank:delete']"
        >删除
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-check"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleCheck"-->
<!--          v-hasPermi="['operation:detail']"-->
<!--        >详情-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="single"
          @click="handleCopy"
          v-hasPermi="['pay:rank:copy']"
        >复制
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rankList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center"/>
      <!--      <el-table-column label="序号" align="center" type="index"/>-->
      <el-table-column label="职级名称" align="center" prop="rankName" width="190px"/>
      <el-table-column label="工资标准" align="center" prop="wageStandard"/>
      <el-table-column label="岗位补贴" align="center" prop="rankSubsidy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140px" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleCheck(scope.row)"
            v-hasPermi="['pay:rank:detail']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['pay:rank:edit']"
          >编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据编号" prop="orderCode">
          <span>{{ form.orderCode }}</span>
        </el-form-item>
        <el-form-item label="审核结果" prop="auditResult">
          <el-select v-model="form.auditResult" placeholder="请选择" clearable size="small">
            <!--            <el-option label="待审核" value="1"></el-option>-->
            <el-option label="审核通过" value="2"></el-option>
            <el-option label="审核拒绝" value="3"></el-option>
            <el-option label="审核中" value="4"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

  import { updateAuditInfo, delOperationalInfo, submitAudit, delAudit} from "@/api/operation";
  import { getPayRankList, deletePayRank } from "@/api/personnel/pay/rank";

  export default {
    name: "Scheme",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 职级列表
        rankList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          rankName: undefined,
        },
        //提交审核表单
        auditParam: {
          processInstanceName: undefined,
          businessKey: undefined,
          processDefinitionKey: 'yyxq_audit',
          variables: undefined,
          processType: 1
        },
        //审核撤销
        delAuditParam: {
          taskId: undefined,
          reason: undefined,
          processType: 1,
        },
        //审核参数
        variablesMap: {
          auditFlag: undefined,
          orderCode: undefined
        },
        // 表单参数
        form: {
          id: undefined,
          orderCode: undefined,
          auditResult: undefined
        },
        // 表单校验
        rules: {
          auditResult: [
            {required: true, message: "请选择审核结果", trigger: "change"}
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        getPayRankList(this.queryParams).then(response => {
          console.log(response)
          this.rankList = response.rows;
          this.total = response.total;
          this.loading = false;
        }).catch(e=>{
          console.log(e)
        });
      },
      // 岗位状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          postId: undefined,
          postCode: undefined,
          postName: undefined,
          postSort: 0,
          status: "0",
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$router.push({path: "/basic/rankAdd"});

      },
      /** 详情按钮操作 */
      handleEdit(row) {
        // console.log(row);
        this.$router.push({
          path: "/basic/rankEdit",
          query: row
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            // console.log(valid,this.form);
            const params = {
              id: this.form.id,
              auditResult: this.form.auditResult
            }
            // console.log(params)
            //审核
            this.updateAuditInfo(params);
            // updateAuditInfo().then(res=>{
            //   console.log(res)
            // })
          }
        });
      },
      updateAuditInfo(params) {
        updateAuditInfo(params).then(res => {
          // console.log(res);
          if (res.code === 200) {
            this.msgSuccess(res.msg);
            this.open = false;
            //重新获取列表
            this.queryParams.pageNum = 1;
            this.getList()
          } else {
            this.msgError(res.msg || '审核提交错误')
          }
        })
      },
      /** 详情按钮操作 */
      handleCopy(row) {
        const id =  this.ids[0];
        const data = this.rankList.filter(item => item.id === id);
        const params = data[0];
        this.$router.push({path: "/basic/rankCopy", query: { ...params, type: 'copy' } });
      },
      /** 详情按钮操作 */
      handleCheck(row) {
        const params = row;
        this.$router.push({path: "/basic/rankDetail", query: params });
      },
      /** 审核按钮操作 */
      handleAudit(row) {
        this.title = "运营需求单审核";
        this.open = true;
        this.form = {
          ...this.form,
          id: row.id,
          orderCode: row.orderCode,
        }
        // console.log(row)
      },
      /**提交审核**/
      submitOperationalAudit(row) {
        this.loading = true;
        let standardNumberLimit =(row.standardNumber/row.businessClassification)* row.predictNumber;
        let standardNumberLimitResult = standardNumberLimit* (1 + row.controlRatio / 100);
        let estimatedLaborNumber = row.estimatedLaborNum;
        let flowFlag = estimatedLaborNumber > standardNumberLimitResult ? 1 : 0;
        console.log(standardNumberLimit)
        console.log(estimatedLaborNumber)
        console.log(standardNumberLimitResult);
        this.variablesMap.auditFlag = flowFlag;
        this.variablesMap.orderCode = row.orderCode;
        this.auditParam.businessKey = row.id;
        this.auditParam.processInstanceName = row.workTime + " 运营需求单";
        this.auditParam.variables = this.variablesMap;
        submitAudit(this.auditParam).then(res => {
          this.loading = false;
          if (res.code === 200) {
            this.msgSuccess(res.msg || '审核提交成功');
            this.getList();
          } else {
            this.msgError(res.msg || '审核提交错误');
          }
        })
      },
      /**审核撤回**/
      delOperationalAudit(row) {
        this.loading = true;
        this.delAuditParam.taskId = row.id;
        this.delAuditParam.reason = "任务撤销";
        delAudit(this.delAuditParam).then(res => {
          this.loading = false;
          if (res.code === 200) {
            this.msgSuccess(res.msg || '审核撤销成功');
            this.getList();
          } else {
            this.msgError(res.msg || '审核撤销错误');
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        // console.log(row)
        const ids = row.id || this.ids;
        const _this = this;
        this.$confirm('是否确认删除需求单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          const params = ids.join(',')
          _this.deletePayRank(params);
        }).catch((e) => {
          console.log(e)
        });
      },
      //删除接口
      deletePayRank(ids) {
        deletePayRank(ids).then(res => {
          // console.log(res)
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
    }
  };
</script>
